package com.rm2pt.generator.springboot.LabManage.service;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.rm2pt.generator.springboot.LabManage.entity.*;
import com.rm2pt.generator.springboot.LabManage.utils.DaoManage;
import com.rm2pt.generator.springboot.LabManage.utils.PreconditionException;
import com.rm2pt.generator.springboot.LabManage.utils.ServiceManage;
import com.rm2pt.generator.springboot.LabManage.utils.StandardOPs;
import com.rm2pt.generator.springboot.LabManage.redis.CurrentUtils;

@Transactional
@Service
public class CreateTeamService{
	@Autowired CurrentUtils currentUtils;
	@Autowired private DaoManage DM;
		
	@Autowired private ServiceManage serviceManage;

	public Boolean createTeam(String id,String userId,String name,String message) throws PreconditionException{
		User user = (User)GetData(DM.getUserDao().findById(userId));
		Team team = (Team)GetData(DM.getTeamDao().findById(id));
		
		if(StandardOPs.oclIsUndefined(user) == false && user.getIdentity() == IdentityType.TEACHER.ordinal() && StandardOPs.oclIsUndefined(team) == true)
		{
			Team t = new Team();
			t.setId(id);
			t.setBelongTeacher(userId);
			t.setName(name);
			t.setIntroduce(message);
			DM.getTeamDao().save(t);
			return true;
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
	public Boolean createGroup(String id,String teamId,String name,String message) throws PreconditionException{
		Team team = (Team)GetData(DM.getTeamDao().findById(teamId));
		Group group = (Group)GetData(DM.getGroupDao().findById(id));
		
		if(StandardOPs.oclIsUndefined(team) == false && StandardOPs.oclIsUndefined(group) == true)
		{
			Group g = new Group();
			g.setId(id);
			g.setBelongTeam(teamId);
			g.setName(name);
			g.setIntroduce(message);
			DM.getGroupDao().save(g);
			return true;
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
	public Boolean handleTeamApply(String teamId,Boolean agree) throws PreconditionException{
		Apply apply = DM.getApplyDao().findByAimIdAndApplyType(teamId,ApplyType.JOINTEAM);
		
		if(StandardOPs.oclIsUndefined(apply) == false)
		{
			StudentInTeam sit = new StudentInTeam();
			if(agree == true){
				sit.setTeamId(teamId);
			sit.setStudentId(apply.getBelongStudent());
			DM.getStudentInTeamDao().save(sit);
			DM.getApplyDao().delete(apply);
			return true;
			
			}else{
				DM.getApplyDao().delete(apply);
			return true;
			
			}
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
	public Boolean handleGroupApply(String groupId,Boolean agree) throws PreconditionException{
		Apply apply = DM.getApplyDao().findByAimIdAndApplyType(groupId,ApplyType.JOINGROUP);
		
		if(StandardOPs.oclIsUndefined(apply) == false)
		{
			StudentInGroup sig = new StudentInGroup();
			if(agree == true){
				sig.setGroupId(groupId);
			sig.setStudentId(apply.getBelongStudent());
			DM.getStudentInGroupDao().save(sig);
			DM.getApplyDao().delete(apply);
			return true;
			
			}else{
				DM.getApplyDao().delete(apply);
			return true;
			
			}
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
	public Boolean dismissGroup(String groupId) throws PreconditionException{
		Group group = (Group)GetData(DM.getGroupDao().findById(groupId));
		StudentInGroup sig = DM.getStudentInGroupDao().findByGroupId(groupId);
		
		if(StandardOPs.oclIsUndefined(group) == false && StandardOPs.oclIsUndefined(sig) == true)
		{
			DM.getGroupDao().delete(group);
			return true;
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
	public Boolean dismissTeam(String teamId) throws PreconditionException{
		Team team = (Team)GetData(DM.getTeamDao().findById(teamId));
		StudentInTeam sit = DM.getStudentInTeamDao().findByTeamId(teamId);
		List<Group> groups = DM.getGroupDao().findByBelongTeam(teamId);
		
		if(StandardOPs.oclIsUndefined(team) == false && StandardOPs.oclIsUndefined(sit) == true && groups.size() != 0)
		{
			DM.getTeamDao().delete(team);
			return true;
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
				
	public static Object GetData(Optional<?> op) {
		if (op.isPresent())
			return op.get();
		else 
			return null;
	}
}
